package Principal;



import java.io.*; 

import javax.swing.JFrame;
import javax.swing.WindowConstants;

import org.antlr.runtime.CharStream;
import org.antlr.runtime.ANTLRFileStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.Token;
import org.antlr.runtime.TokenRewriteStream;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeAdaptor;
import org.antlr.runtime.tree.DOTTreeGenerator;
import org.antlr.runtime.tree.TreeAdaptor;
import org.antlr.stringtemplate.StringTemplate;

import antlr.ANTLRException;



import MiArbol.MiArbol;
import MiArbol.MiArbolAdaptor;


public class Principal { 
	
	
	public Principal(String arg) throws ANTLRException {
	
			LeerFichero(arg);
	}

	public Principal() {
		// TODO Auto-generated constructor stub
	}

	
	private static void LeerFichero(String arg) {
		try { 
			CharStream fis = null;
			fis = new ANTLRFileStream(arg);
			//FileInputStream fis = new FileInputStream(arg); 
			Analex analex = null;	 
			Anasin anasint = null; 
			analex = new Analex (fis); 
			anasint = new Anasin (new CommonTokenStream(analex));
			CommonTree arbol = null;
			arbol = (CommonTree) anasint.instrucciones().getTree();
			System.out.println ("estamos dentro de principal no de los testPrincipal");
			System.out.println ("Nuestro arbol AST --> " + arbol.toStringTree());	
			VentanaAST ventana = new VentanaAST("Tree", arbol);
			ventana.setVisible(true);
			ventana.setSize(200, 200);

			ventana.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		}catch(RecognitionException ae) { 
			System.err.println(ae.getMessage()); 
		} 
		catch(IOException e ) { 
			System.err.println("No se encontro el fichero"); 
		}
	}
	
	private static void LeerFichero2(String arg) {
		try { 
			CharStream fis = null;
			fis = new ANTLRFileStream(arg);
			//FileInputStream fis = new FileInputStream(arg); 
			Analex analex = null;	 
			analex = new Analex (fis);
			
			TokenRewriteStream tokens = new TokenRewriteStream(analex);
			AnasinAmpliado anasint = new AnasinAmpliado (tokens);
			anasint.setTreeAdaptor (new MiArbolAdaptor (arg));				
			AnasinAmpliado.instrucciones_return ret = anasint.instrucciones();		
			MiArbol tree = (MiArbol)ret.getTree();
			//CommonTree tree = null;
			//tree = (CommonTree) ret.getTree();
			
			if (tree != null) {
			System.out.println ("Nuestro arbol AST --> " + tree.toStringTree());	
			VentanaAST ventana = new VentanaAST("Tree", (CommonTree)tree);
			ventana.setVisible(true);
			ventana.setSize(200, 200);
			ventana.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			}
			else 
				System.out.println ("Arbol vacio");	
		}catch(RecognitionException ae) { 
			System.err.println(ae.getMessage()); 
		} 
		catch(IOException e ) { 
			System.err.println("No se encontro el fichero"); 
		}
	}

	private static void LeerFichero3(String arg) {
		try { 
			CharStream fis = null;
			fis = new ANTLRFileStream(arg);
			//FileInputStream fis = new FileInputStream(arg); 
			Analex analex = null;	 
			analex = new Analex (fis);
			
			TokenRewriteStream tokens = new TokenRewriteStream(analex);
			AnasinMiArbol anasint = new AnasinMiArbol (tokens);
			anasint.setTreeAdaptor (new MiArbolAdaptor (arg));				
		    AnasinMiArbol.instrucciones_return ret = anasint.instrucciones();
		
			MiArbol tree = (MiArbol)ret.getTree();
			//CommonTree tree = null;
			//tree = (CommonTree) ret.getTree();
			
			if (tree != null) {
			System.out.println ("Nuestro arbol AST --> " + tree.toStringTree());	
			VentanaAST ventana = new VentanaAST("Tree", (CommonTree)tree);
			ventana.setVisible(true);
			ventana.setSize(200, 200);
			ventana.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			}
			else 
				System.out.println ("Arbol vacio");	
		}catch(RecognitionException ae) { 
			System.err.println(ae.getMessage()); 
		} 
		catch(IOException e ) { 
			System.err.println("No se encontro el fichero"); 
		}
	}
	
	private static void LeerFichero4(String arg) {
		try { 
			CharStream fis = null;
			fis = new ANTLRFileStream(arg);
			//FileInputStream fis = new FileInputStream(arg); 
			Analex analex = null;	 
			analex = new Analex (fis);
			
			TokenRewriteStream tokens = new TokenRewriteStream(analex);
			AnasinC anasint = new AnasinC (tokens);
			anasint.setTreeAdaptor (new MiArbolAdaptor (arg));		
		
			AnasinC.entrada_return ret = anasint.entrada();
		
			MiArbol tree = (MiArbol)ret.getTree();
			//CommonTree tree = null;
			//tree = (CommonTree) ret.getTree();
			
			if (tree != null) {
			System.out.println ("Nuestro arbol AST --> " + tree.toStringTree());	
			VentanaAST ventana = new VentanaAST("Tree", (CommonTree)tree);
			ventana.setVisible(true);
			ventana.setSize(200, 200);
			ventana.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			}
			else 
				System.out.println ("Arbol vacio");	
		}catch(RecognitionException ae) { 
			System.err.println(ae.getMessage()); 
		} 
		catch(IOException e ) { 
			System.err.println("No se encontro el fichero"); 
		}
	}
	
	private static void LeerFichero5(String arg) {
		try { 
			CharStream fis = null;
			fis = new ANTLRFileStream(arg);
			//FileInputStream fis = new FileInputStream(arg); 
			Analex analex = null;	 
			analex = new Analex (fis);
			
			TokenRewriteStream tokens = new TokenRewriteStream(analex);
			AnasinC anasint = new AnasinC (tokens);
			anasint.setTreeAdaptor (new MiArbolAdaptor (arg));		
		
			AnasinC.entrada_return ret = anasint.entrada();
		
			MiArbol tree = (MiArbol)ret.getTree();
			//CommonTree tree = null;
			//tree = (CommonTree) ret.getTree();
			
			if (tree != null) {
			System.out.println ("Nuestro arbol AST --> " + tree.toStringTree());	
			VentanaAST ventana = new VentanaAST("Tree", (CommonTree)tree);
			ventana.setVisible(true);
			ventana.setSize(200, 200);
			ventana.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			}
			else 
				System.out.println ("Arbol vacio");	
		}catch(RecognitionException ae) { 
			System.err.println(ae.getMessage()); 
		} 
		catch(IOException e ) { 
			System.err.println("No se encontro el fichero"); 
		}
	}
	
	private static void LeerFichero6(String arg) {
		try { 
			CharStream fis = null;
			fis = new ANTLRFileStream(arg);
			//FileInputStream fis = new FileInputStream(arg); 
			
			
			AnalexXML analex = new AnalexXML (fis); 			
			TokenRewriteStream tokens = new TokenRewriteStream(analex);
			AnasintXML anasint = new AnasintXML (tokens);			
			anasint.setTreeAdaptor (new MiArbolAdaptor (arg));			
			AnasintXML.document_return ret = anasint.document();
		
			MiArbol tree = (MiArbol)ret.getTree();
			//CommonTree tree = null;
			//tree = (CommonTree) ret.getTree();
			
			if (tree != null) {
			System.out.println ("Nuestro arbol AST --> " + tree.toStringTree());	
			VentanaAST ventana = new VentanaAST("Tree", (CommonTree)tree);
			ventana.setVisible(true);
			ventana.setSize(200, 200);
			ventana.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			}
			else 
				System.out.println ("Arbol vacio");	
		}catch(RecognitionException ae) { 
			System.err.println(ae.getMessage()); 
		} 
		catch(IOException e ) { 
			System.err.println("No se encontro el fichero"); 
		}
	}
	
	public static void main(String args[]) { 
	
		System.out.println(args[0].charAt(4));
		switch(args[0].charAt(4)) {
		
			case '1': LeerFichero(args[0]);break;
			case '2': LeerFichero2(args[0]);break;
			case '3': LeerFichero3(args[0]);break;
			case '4': LeerFichero4(args[0]);break;
			case '5': LeerFichero5(args[0]);break;
			case '6': LeerFichero6(args[0]);break;
		}
		//LeerFichero(args[0]);
		
	} 
	
}
	